﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;

namespace WoTStats.ClientApplication
{
    public partial class ServerStatus : UserControl
    {
        public List<string> IPAddresses { get; set; }
        public string ServerName { get; set; }

        public ServerStatus()
        {
            InitializeComponent();

            IPAddresses = new List<string>();
        }

        public void Ping()
        {
            lblServerName.Text = ServerName;
            PingIPAddresses();
        }

        private void PingIPAddresses()
        {
            Ping pingSender = new Ping();
            PingOptions pingOptions = new PingOptions(128, true);

            byte[] buffer = Encoding.ASCII.GetBytes("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

            int successCount = 0;

            foreach(string addr in IPAddresses)
            {
                PingReply reply = pingSender.Send(addr, 120, buffer, pingOptions);

                if (reply.Status == IPStatus.Success)
                {
                    successCount += 1;
                    lblServerName.Text = String.Format("{0} : {1}ms", ServerName, reply.RoundtripTime);
                }
            }

            if (successCount == IPAddresses.Count())
            {
                pbStatusImg.Image = new Bitmap("Graphics\\greencircle.png");
            }
            else if (successCount > 0 && successCount < IPAddresses.Count())
            {
                pbStatusImg.Image = new Bitmap("Graphics\\yellowcircle.png");
            }
            else
            {
                pbStatusImg.Image = new Bitmap("Graphics\\redcircle.png");
            }

        }
    }
}
